package org.linlinjava.litemall.db.util;

import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;

/**
 * 日期格式化工具类
 */
public class DateTimeUtil {

    /**
     * 格式 yyyy年MM月dd日 HH:mm:ss
     *
     * @param dateTime
     * @return
     */
    public static String getDateTimeDisplayString(LocalDateTime dateTime) {
        DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
        String strDate2 = dtf2.format(dateTime);

        return strDate2;
    }


    /**
     * 获取指定时间所在年的开始时间和结束时间
     *
     * @param dateTime 指定的时间
     * @return 包含指定时间所在年开始时间和结束时间的数组，数组的第一个元素为开始时间，第二个元素为结束时间
     */
    public static LocalDateTime[] getYearStartEnd(LocalDateTime dateTime) {
        LocalDateTime yearStart = dateTime.withMonth(Month.JANUARY.getValue())
                .withDayOfMonth(1)
                .withHour(0)
                .withMinute(0)
                .withSecond(0)
                .withNano(0);

        LocalDateTime yearEnd = dateTime.withMonth(Month.DECEMBER.getValue())
                .with(TemporalAdjusters.lastDayOfMonth())
                .withHour(23)
                .withMinute(59)
                .withSecond(59)
                .withNano(999999999);

        return new LocalDateTime[]{yearStart, yearEnd};
    }

    /**
     * 获取指定时间所在月的开始时间和结束时间
     *
     * @param dateTime 指定的时间
     * @return 包含指定时间所在月开始时间和结束时间的数组，数组的第一个元素为开始时间，第二个元素为结束时间
     */
    public static LocalDateTime[] getMonthStartEnd(LocalDateTime dateTime) {
        LocalDateTime monthStart = dateTime.withDayOfMonth(1)
                .withHour(0)
                .withMinute(0)
                .withSecond(0)
                .withNano(0);

        LocalDateTime monthEnd = dateTime.with(TemporalAdjusters.lastDayOfMonth())
                .withHour(23)
                .withMinute(59)
                .withSecond(59)
                .withNano(999999999);

        return new LocalDateTime[]{monthStart, monthEnd};
    }

    /**
     * 获取指定时间所在周的开始时间和结束时间
     *
     * @param dateTime 指定的时间
     * @return 包含指定时间所在周开始时间和结束时间的数组，数组的第一个元素为开始时间，第二个元素为结束时间
     */
    public static LocalDateTime[] getWeekStartEnd(LocalDateTime dateTime) {
        LocalDateTime weekStart = dateTime.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY))
                .withHour(0)
                .withMinute(0)
                .withSecond(0)
                .withNano(0);

        LocalDateTime weekEnd = dateTime.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY))
                .with(TemporalAdjusters.next(DayOfWeek.MONDAY))
                .withHour(23)
                .withMinute(59)
                .withSecond(59)
                .withNano(999999999);

        return new LocalDateTime[]{weekStart, weekEnd};
    }



    public static LocalDateTime[] getDayStartEnd(LocalDateTime dateTime) {
        LocalDateTime dayStart = dateTime.withHour(0)
                .withMinute(0)
                .withSecond(0)
                .withNano(0);

        LocalDateTime dayEnd = dateTime.withHour(23)
                .withMinute(59)
                .withSecond(59)
                .withNano(999999999)
                .truncatedTo(java.time.temporal.ChronoUnit.SECONDS);
        return new LocalDateTime[]{dayStart, dayEnd};
    }


    public static LocalDateTime[] getLocaTimeStartEnd(Integer type){
        LocalDateTime[] localDateTimes = null;
        if(type==0){
            localDateTimes =getYearStartEnd(LocalDateTime.now());
        }else if(type==1){
            localDateTimes =getMonthStartEnd(LocalDateTime.now());
        }else if(type==2){
            localDateTimes =getWeekStartEnd(LocalDateTime.now());
        }else if(type==3){
            localDateTimes =getDayStartEnd(LocalDateTime.now());
        }
        return localDateTimes;
    }



}
